//
//  WAGoodsListController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/21.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WAGoodsListController.h"
#import "WAGoodsCell.h"
#import "WAGoodDetailController.h"
#import "WAGoodModel.h"

@interface WAGoodsListController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (strong, nonatomic) NSMutableArray *dataSouces;
@property (assign, nonatomic) NSInteger pageNum;
@property (assign, nonatomic) NSInteger companyID;
@end

@implementation WAGoodsListController
- (instancetype)initWithID:(NSInteger )companyID
{
    self = [super init];
    if (self) {
        self.companyID = companyID;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    CGFloat padding = 15;
    _layout.minimumLineSpacing = padding;//上册cell的距离
    _layout.sectionInset = UIEdgeInsetsMake(padding, padding, 0, padding);
    CGFloat item_W = (NSInteger)((WNScreenWidth - padding * 3 - 2)/2);
    CGFloat item_H = item_W * 600./482.f;
    _layout.itemSize = CGSizeMake( item_W, item_H);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    [self.collectionView registerNib:[UINib nibWithNibName:@"WAGoodsCell" bundle:nil] forCellWithReuseIdentifier:@"WAGoodsCell"];
    @weakify(self)
    self.collectionView.mj_header = [MJRefreshLoadHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum = 1;
        [self requestData];
    }];
    self.collectionView.mj_footer = [MJRefreshMoreFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum ++;
        [self requestData];
    }];
    [self.collectionView.mj_header beginRefreshing];
}

- (void)requestData{
    [WNBusinessRequest getGoodsListWithCompanyID:[@(self.companyID) stringValue] pageNum:self.pageNum Succ:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSInteger total = [dict[@"total"] integerValue];
        NSArray *memberList = [WAGoodModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (self.pageNum == 1) {
            self.dataSouces = [NSMutableArray new];
        }
        for (WAGoodModel * model in memberList) {
            [self.dataSouces addObject:model];
        }
        if(total <= self.dataSouces.count){
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.collectionView.mj_footer endRefreshing];
        }
        [self.collectionView.mj_header endRefreshing];
        if (self.dataSouces.count == 0) {
            self.noDataStr = @"无数据";
        }else{
            [self.collectionView reloadData];
        }
        [self.collectionView reloadData];
        [self.collectionView reloadEmptyDataSet];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        self.noDataStr = mes;
        [self.collectionView reloadEmptyDataSet];
        [self.collectionView.mj_footer endRefreshing];
        [self.collectionView.mj_header endRefreshing];
        [WNTool show:mes];
    }];
}

#pragma mark - UICollectionView Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSouces.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WAGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WAGoodsCell" forIndexPath:indexPath];
    [cell setModel:self.dataSouces[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WAGoodModel *model = self.dataSouces[indexPath.row];
    WAGoodDetailController *VC = [[WAGoodDetailController alloc] initWithID:[@(model.ID) stringValue]];
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - DZNEmptyDataSetSource
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self.collectionView.mj_header beginRefreshing];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    return [WNTool requestErrorWithSting:self.noDataStr];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
